home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / array.lha / array / array.C next >
Text File  |  1993-08-08  |  2KB  |  88 lines

  1. / Implementation of template array class, fixed size.
  2. /
  3. / Assertions can be turned off by defining NDEBUG, see assert(3).
  4. /
  5. / Author: Dag Bruck, Department of Automatic Control, Lund Institute of
  6. / Technology, Box 188, S-221 00 Lund, Sweden.  E-mail: dag@control.lth.se
  7. /
  8. / $Id: array.C,v 1.6 1992/07/07 12:39:02 dag Exp $
  9.  
  10. include <array.H>
  11. include <assert.h>
  12.  
  13. emplate <class T>
  14. rray<T>::Array(int l, int h)
  15.     : low(l), sz(h-l+1)
  16.  
  17.   assert(h >= l);
  18.   data = new T[sz];
  19.   assert(data != 0);
  20.  
  21.  
  22. emplate <class T>
  23. rray<T>::~Array()
  24.  
  25.   delete [] data;
  26.  
  27.  
  28. emplate <class T>
  29. & Array<T>::operator [] (int i)
  30.  
  31.   assert(i >= low);
  32.   unsigned index = i - low;
  33.   assert(index < sz);
  34.   return data[index];
  35.  
  36.  
  37. emplate <class T>
  38. onst T& Array<T>::operator [] (int i) const
  39.  
  40.   return ((Array<T>*) this)->operator [] (i);
  41.  
  42.  
  43. emplate <class T>
  44. rray<T>& Array<T>::operator = (const T& x)
  45.  
  46.   for (unsigned i = 0; i < sz; i++) data[i] = x;
  47.   return *this;
  48.  
  49.  
  50. emplate <class T>
  51. rray<T>& Array<T>::operator = (const Array<T>& a)
  52.  
  53.   if (this != &a) {
  54.     assert(low == a.low && sz == a.sz);
  55.     for (unsigned i = 0; i < sz; i++) data[i] = a.data[i];
  56.   }
  57.   return *this;
  58.  
  59.  
  60. emplate <class T>
  61. oid Array<T>::bounds(int& lower_bound, int& upper_bound) const
  62.  
  63.   lower_bound = low;
  64.   upper_bound = low + sz - 1;
  65.  
  66.  
  67.  
  68. ifdef TEST
  69. include <stream.h>
  70.  
  71. truct Foo {
  72.   Foo() : x(0) { cout << "making a Foo @ " << (void*) this << endl; }
  73.   Foo(const Foo& f) : x(f.x) {}
  74.   void operator=(const Foo& f) { x = f.x; }
  75.   ~Foo() { cout << "destroying a Foo @ " << (void*) this << endl; }
  76.   int x;
  77. ;
  78.  
  79. ain()
  80.  
  81.   Foo f;
  82.   Array<Foo> a(1, 5);
  83.   a = f;
  84.   Array<Foo> b(1, 5);
  85.   b = a;;
  86.  
  87. endif
  88.